package com.gxa.global.realm;

import com.gxa.entity.CitySysUser;
import com.gxa.entity.DistrictSysPermission;
import com.gxa.entity.DistrictSysRole;
import com.gxa.entity.DistrictSysUser;
import com.gxa.entity.SysActionLog;
import com.gxa.mapper.DistrictSysUserMapper;
import com.gxa.mapper.DistrictUserMapper;
import com.gxa.mapper.PermissionMapper;
import com.gxa.mapper.RoleMapper;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class DistrictRealm extends AuthorizingRealm {

    @Autowired
    private DistrictSysUserMapper districtSysUserMapper;

    @Autowired
    private DistrictUserMapper districtUserMapper;

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private PermissionMapper permissionMapper;

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        System.out.println("==进入授权==");
        //获取登录成功后的账户名-- 根据账户名查询对应的账户id
        String nickname = String.valueOf(principalCollection.getPrimaryPrincipal());
        //根据账户名查询账户信息
        DistrictSysUser districtSysUser = districtUserMapper.selectDistrictUserByNickname(nickname);
        String role = districtSysUser.getRole();
        //放入Set集合中去重
        Set<String> roleSet = new HashSet<>();
        roleSet.add(role);
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        //授权--授角色
        info.setRoles(roleSet);
        //根据角色名称查询角色对应的权限信息
        DistrictSysRole districtSysRole = roleMapper.selectRoleByName(role);
        System.out.println(districtSysRole);
        List<DistrictSysPermission> districtSysPermissionList = permissionMapper.selectPermissionsByRoleId(districtSysRole.getRoleId());
        if(districtSysPermissionList.size() > 0){
            //放入Set集合中去重
            Set<String> permissionSet = new HashSet<>();
            for(DistrictSysPermission districtSysPermission : districtSysPermissionList){
                permissionSet.add(districtSysPermission.getPermissionName());
            }
            info.setStringPermissions(permissionSet);
        }
        return info;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        System.out.println("====进入认证方法====");
        //获取客户端传递的账号和密码
        String nickName = String.valueOf(authenticationToken.getPrincipal());

        DistrictSysUser districtSysUser = districtSysUserMapper.findSysUserByNickname(nickName);

        if (districtSysUser !=null){
            //将数据库中的账户和密码与客户端传递的账户密码进行比较
            SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(districtSysUser.getNickname(),districtSysUser.getPassword(),"districtRealm");
            //加盐值
            info.setCredentialsSalt(ByteSource.Util.bytes(districtSysUser.getSalt()));

            return  info;
        }
        return null;
    }
}
